﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace SteeringBehaviors
{
	// Token: 0x02000219 RID: 537
	public class WaypointArrive : SteerBehavior
	{
		// Token: 0x06000B48 RID: 2888 RVA: 0x0005EDE8 File Offset: 0x0005CFE8
		public WaypointArrive(MovementCharacter agent, IKinematic target) : base(agent, target)
		{
			this.Weight = 2f;
			this.goalArrive = new Arrive(agent, target);
			this.waypointsQueue = new Queue<IKinematic>();
			this.width = agent.tileWidth;
		}

		// Token: 0x06000B49 RID: 2889 RVA: 0x0005EE78 File Offset: 0x0005D078
		public override SteeringOutput GetSteering()
		{
			this.scale = this.agent.entity.Height;
			float num = this.scale * this.width * 4f;
			if (this.seekWaypoint != null)
			{
				this.Weight = this.seekWeight;
				SteeringOutput steering = this.seekWaypoint.GetSteering();
				if ((this.agent.transform.position - this.currentPoint.position).magnitude < num)
				{
					this.seekWaypoint = null;
				}
				return steering;
			}
			if (this.waypointsQueue.Count > 0)
			{
				this.currentPoint = this.waypointsQueue.Dequeue();
				Debug.DrawRay(this.currentPoint.position, Vector3.up * this.scale / 5f, Color.yellow, 5f);
				this.seekWaypoint = new Seek(this.agent, this.currentPoint, 0f, 0f);
				this.Weight = this.seekWeight;
				return this.seekWaypoint.GetSteering();
			}
			float magnitude = (this.target.position - this.agent.transform.position).magnitude;
			if (magnitude > num * 1.2f)
			{
				this.FindPathToTarget();
			}
			this.Weight = this.arriveWeight;
			return this.goalArrive.GetSteering();
		}

		// Token: 0x06000B4A RID: 2890 RVA: 0x0005EFEC File Offset: 0x0005D1EC
		internal void PurgeSteeringQueue()
		{
			this.waypointsQueue.Clear();
			this.seekWaypoint = null;
		}

		// Token: 0x06000B4B RID: 2891 RVA: 0x0005F000 File Offset: 0x0005D200
		internal int QueueLength()
		{
			return this.waypointsQueue.Count;
		}

		// Token: 0x06000B4C RID: 2892 RVA: 0x0005F010 File Offset: 0x0005D210
		private void FindPathToTarget()
		{
			float time = Time.time;
			if (this.ignorePathfinding)
			{
				this.Weight = 0.5f;
				return;
			}
			if (time < this.lastPathfind + this.timeTolerance)
			{
				this.width *= 1.5f;
				this.tries++;
			}
			else
			{
				this.tries = 0;
				this.width = this.agent.tileWidth;
			}
			if (this.tries > this.maxTry)
			{
				this.ignorePathfinding = true;
			}
			this.lastPathfind = time;
			this.waypointsQueue.Clear();
			List<IKinematic> list = Pathfinder.instance.PlanRoute(this.agent.transform.position, this.target.position, this.scale, this.scale * this.width, this.scale * 0.1f, 0f);
			if (list.Count > 0)
			{
				foreach (IKinematic item in list)
				{
					this.waypointsQueue.Enqueue(item);
				}
			}
		}

		// Token: 0x04001058 RID: 4184
		private Queue<IKinematic> waypointsQueue;

		// Token: 0x04001059 RID: 4185
		private IKinematic currentPoint;

		// Token: 0x0400105A RID: 4186
		private Arrive goalArrive;

		// Token: 0x0400105B RID: 4187
		private Seek seekWaypoint;

		// Token: 0x0400105C RID: 4188
		private float lastPathfind = -99f;

		// Token: 0x0400105D RID: 4189
		private float timeTolerance = 1f;

		// Token: 0x0400105E RID: 4190
		private float width = 0.2f;

		// Token: 0x0400105F RID: 4191
		private bool ignorePathfinding;

		// Token: 0x04001060 RID: 4192
		private int maxTry = 10;

		// Token: 0x04001061 RID: 4193
		private int tries;

		// Token: 0x04001062 RID: 4194
		private float scale = 1.6f;

		// Token: 0x04001063 RID: 4195
		private float seekWeight = 2f;

		// Token: 0x04001064 RID: 4196
		private float arriveWeight = 0.5f;
	}
}
